﻿namespace Pyramid.Hardware.Interop
{
    using System.Collections.Generic;
    using System.Linq;

    public class VariableInputBus
    {
        private readonly IEnumerable<VariableInputLine> outputBus;

        public VariableInputBus(int busSize)
        {
            var output = new List<VariableInputLine>();

            for (int lineIndex = 0; lineIndex < busSize; lineIndex++)
            {
                output.Add(new VariableInputLine());
            }

            this.outputBus = output;
        }

        public void SetValue(ITernary value)
        {
            int lineIndex = 0;

            var trits = value.ToTritArray();

            foreach (var line in outputBus)
            {
                if (lineIndex >= trits.Length)
                {
                    return;
                }

                line.SetInput(trits[lineIndex]);

                lineIndex++;
            }
        }

        public IEnumerable<ILine> OutputBus
        {
            get
            {
                return outputBus.Cast<ILine>();
            }
        }
    }
}
